package cn.swu.lyx.homework2

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class UserAdapter(val users:ArrayList<User>):RecyclerView.Adapter<UserAdapter.UserViewHolder>(){
    inner class UserViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        val ID:TextView=itemView.findViewById(R.id.userID)
        val image:ImageView=itemView.findViewById(R.id.image)
        val name:TextView=itemView.findViewById(R.id.name)
        val sex:TextView=itemView.findViewById(R.id.sex)
        val tel:TextView=itemView.findViewById(R.id.tel)
        val type:TextView=itemView.findViewById(R.id.type)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        val view=LayoutInflater.from(parent.context).inflate(R.layout.item_user,parent,false)
        return UserViewHolder(view)
    }

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        val user=users[position]
        holder.image.setImageResource(user.image)
        holder.ID.text=user.ID
        holder.name.text=user.name
        holder.sex.text=user.sex
        holder.tel.text=user.tel
        holder.type.text=user.type
    }

    override fun getItemCount(): Int {
        return users.size
    }
}